/*---------------------------------------------------------
 * FILE: StrutsReportAction.java
 * LOGICAL NAME: StrutsReportAction
 * PRODUCT: jcel-util
 *----------------------------------------------------------
 */

package bila.framework.web.action;

import java.sql.Connection;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import bila.framework.commons.Constants;
import bila.framework.commons.GeneralFunction;
import bila.framework.reports.jasper.ReportService;

/**
 * @author CEL <b>Created by </b>WJuarez <b>for </b>jcel-util <b>on
 *         </b>02/02/2011 12:16:31 <b>Purpose</b>
 *         <p>
 *         Action generico para la generaci�n de reportes con Struts y
 *         JasperReports
 *         </p>
 */
public class StrutsReportAction<Dto> extends DispatchAction {

	private Log log = LogFactory.getLog(StrutsReportAction.class);
	private String user;
	private String xml;
	private String xpath;
	private HashMap<Object, Object> parameters;
	private Connection conn;

	public ActionForward executeReport(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			String nameReport, String type, String typeDatasource, String nameReportFile)
			throws Exception {
		try {
			ReportService reportServices = new ReportService();

			String reportPath = this.getServlet().getServletContext()
					.getRealPath(nameReport);

			if (!GeneralFunction.isNullOrEmpty(getUser()))
				parameters.put("p_usuario", user);

			HashMap<Object, Object> map = null;
			if (!GeneralFunction.isNullOrEmpty(getParameters()))
				map = getParameters();

			if (typeDatasource.equals(Constants.REPORT_TYPE_JDBC))
				reportServices.executeReportFromJdbc(request, response, map,
						reportPath, type, getConn(), nameReportFile);
			else if (typeDatasource.equals(Constants.REPORT_TYPE_XML))
				reportServices.executeReportFromXml(request, response, map,
						reportPath, typeDatasource, getXml(), getXpath(), nameReportFile);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("Error en executeReport: " + e.getMessage());
			throw e;
		}
		return null;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public HashMap<Object, Object> getParameters() {
		return parameters;
	}

	public void setParameters(HashMap<Object, Object> parameters) {
		this.parameters = parameters;
	}

	public Connection getConn() {
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXpath() {
		return xpath;
	}

	public void setXpath(String xpath) {
		this.xpath = xpath;
	}

}// class
